package com.yer.note.activity.ui;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.TextView;

import com.yer.note.R;
import com.yer.note.activity.nav_label_diary;
import com.yer.note.adapter.LabelDiaryAdapter;
import com.yer.note.entity.Diary;
import com.yer.note.entity.LabelDiary;
import com.yer.note.entity.User;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
 * 主要实现两个功能：
 *         * 1、展示所有标签名
 *         * 2、给每个标签名配上点击事件，点击后会跳转到日记详情页面
 */
public class LabelFragment extends Fragment {
    private View root;
    private ArrayList<LabelDiary> labelDiaries = new ArrayList<>();
    private List<LabelDiary> labelDiariesbackup = new ArrayList<>();
    private RecyclerView recyclerView;
    LabelDiaryAdapter labelDiaryAdapter;
    private SearchView search;
    public void initData(){
        List<Diary> list = Diary.listAll(Diary.class);
        for (int i = 0; i < list.size(); i++) {
            Diary diary = list.get(i);
            if(diary.getUseruuid().equals(User.user.getMyuuid())){
                if(labelDiaries==null || !isExist(diary.getLabel())){
                    LabelDiary labelDiary =  new LabelDiary(-1,1,diary.getLabel());
                    labelDiaries.add(labelDiary);
                }
            }
        }
        labelDiaries.sort(new Comparator<LabelDiary>() {
            @Override
            public int compare(LabelDiary t1, LabelDiary t2) {
                return t1.compareTo(t2);
            }
        });
        for (int i = 0; i < labelDiaries.size(); i++) {
            labelDiaries.get(i).number = i+1;
        }
        labelDiariesbackup.addAll(labelDiaries);
    }
    public boolean isExist(String label){
        for (int i = 0; i < labelDiaries.size(); i++) {
            if(labelDiaries.get(i).getLabel().equals(label)){
                labelDiaries.get(i).amount++;
                return true;
            }
        }
        return false;
    }
    public void initRecyclerview(){
        recyclerView = root.findViewById(R.id.axis_recyclerview);
        labelDiaryAdapter = new LabelDiaryAdapter(getActivity(),labelDiaries);
        recyclerView.setAdapter(labelDiaryAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }
    private TextView label;
    public LabelFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(root==null){
            root = inflater.inflate(R.layout.fragment_label,container,false);
        }
        initData();
        initRecyclerview();
        label = root.findViewById(R.id.item_label);
        search = root.findViewById(R.id.label_search);
        search.setIconifiedByDefault(false);
        search.setSubmitButtonEnabled(true);
        search.setQueryHint("查找");
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                if(TextUtils.isEmpty(s)){
                    labelDiaries.clear();
                    labelDiaries.addAll(labelDiariesbackup);
                }else{
                    for (int i = 0; i < labelDiaries.size(); i++) {
                        if(!labelDiaries.get(i).getLabel().contains(s)){
                            labelDiaries.remove(i);
                        }
                    }
                }
                labelDiaryAdapter.notifyDataSetChanged();
                return false;
            }
        });
        labelDiaryAdapter.setOnItemClickListener(new LabelDiaryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String label) {
                System.out.println(label);
                Intent intent = new Intent(getActivity(), nav_label_diary.class);
                intent.putExtra("label",label);
                startActivity(intent);
            }
        });
        return root;
    }
}